/**
 * This class represents a custom rss feed parser which is used to parse
 * rss feeds for the events calendar module in the iWestern application. 
 * It is a subclass of RssFeedParser and uses methods / attributes from 
 * this class when parsing the rss feed.
 * @author Chirag Mistry
 */
package ca.uwo.android.rss;

import java.io.InputStream;

import android.content.Context;
import android.sax.Element;
import android.sax.EndElementListener;
import android.sax.EndTextElementListener;
import android.sax.RootElement;
import android.util.Xml;

public class SaxEventCalendarParser extends RssFeedParser {

	//namespaces used by elements in this rss feed
	static final String RDF_NAMESPACE = "http://www.w3.org/1999/02/22-rdf-syntax-ns#";
	static final String DC_NAMESPACE = "http://purl.org/dc/elements/1.1/";
	static final String RSS_NAMESPACE = "http://purl.org/rss/1.0/";
	static final String EV_NAMESPACE = "http://purl.org/rss/1.0/modules/event/";
	
	/**
	 * Constructor for the rss feed parser
	 * @param rssFeedURL - the url of the rss feed to parse
	 */
	public SaxEventCalendarParser(String rssFeedURL, Context context) throws Exception{
		super(rssFeedURL, context);
	}

	/**
	 * This method parses the rss feed and returns an rss feed object containing
	 * all the feed items read in from the source
	 * @return
	 */
	public RssFeed parse() throws Exception{
		final RssItem currentItem = new RssItem();	//represents current item being parsed
		
		//define the root element of the rss feed document
		RootElement root = new RootElement(RDF_NAMESPACE, "RDF");
		
		final RssFeed rssFeed = new RssFeed();	//rss feed object to store all the rss items
		
		String rssFeedText;
		
		InputStream is = this.getInputStream();	//get the input stream to the rss feed
//		InputStream is = super.getInputStream();
		rssFeedText = this.convertSpecialChars(is);	//convert special characters in feed	
//		rssFeedText = super.convertSpecialChars(is);
		
		//get each (child) item element from the root and store it in the rss feed object
		Element item = root.getChild(RSS_NAMESPACE, ITEM);
		item.setEndElementListener(new EndElementListener() {
			public void end() {
				rssFeed.add(currentItem.copy());
			}
		});
		
		//parse and store the title of the rss feed item
		item.getChild(RSS_NAMESPACE, TITLE).setEndTextElementListener(
				new EndTextElementListener() {
					public void end(String body) {
						currentItem.setTitle(body);
					}
				});
		
		//parse and store the link of the rss feed item
 		item.getChild(RSS_NAMESPACE, LINK).setEndTextElementListener(
				new EndTextElementListener() {
					public void end(String body) {
						currentItem.setLink(body);
					}
				});
		
		//parse and store the description of the rss feed item
		item.getChild(RSS_NAMESPACE, DESCRIPTION).setEndTextElementListener(
				new EndTextElementListener() {
					public void end(String body) {
						currentItem.setDescription(body);
					}
				});
		
		//parse and store the date of the rss feed item
		item.getChild(DC_NAMESPACE,DATE).setEndTextElementListener(
				new EndTextElementListener() {
					public void end(String body) {
						currentItem.setDate(body);
					}
				});
		

			
		//parse the rss feed as a string (since the special characters had to be replaced)
		Xml.parse(rssFeedText, root.getContentHandler());

		
		return rssFeed;	
	}
	
}
